package util

import (
	"fmt"

	"gopkg.in/ini.v1"
)

// 解析配置文件并设置参数

var (
	AppMode  string
	HttpPort string
	JwtKey   string
	IsServer bool

	Db         string
	DbHost     string
	DbPort     string
	DbUser     string
	DbPassWord string
	DbName     string

	Serurl      string
	IsSync      bool
	SzIP        string
	MoniterTime int

	IsShowDb bool

	PageSize int
)

func init() {
	// 解析配置文件config.ini
	file, err := ini.Load("config/config.ini")
	if err != nil {
		fmt.Printf("配置文件读取错误，请检查文件路径: %v\n", err)
	}
	// 读取服务端相关配置
	LoadServer(file)
	// 读取数据库相关配置
	LoadDate(file)
	// 读取客户端相关配置
	Loadclient(file)

}

func LoadServer(file *ini.File) {
	AppMode = file.Section("server").Key("AppMode").MustString("debug")
	HttpPort = file.Section("server").Key("HttpPort").MustString(":80")
	JwtKey = file.Section("server").Key("JwtKey").MustString("45df45rds4")
	IsServer = file.Section("server").Key("isServer").MustBool(true)
}

func LoadDate(file *ini.File) {
	Db = file.Section("database").Key("Db").MustString("mssql")
	DbHost = file.Section("database").Key("DbHost").MustString("127.0.0.1")
	DbPort = file.Section("database").Key("DbPort").MustString(":1433")
	DbUser = file.Section("database").Key("DbUser").MustString("sa")
	DbPassWord = file.Section("database").Key("DbPassWord").MustString("123456")
	DbName = file.Section("database").Key("DbName").MustString("hydb")
	IsShowDb = file.Section("database").Key("isShowDb").MustBool(true)
}

func Loadclient(file *ini.File) {
	Serurl = file.Section("client").Key("serurl").MustString("http://127.0.0.1:8080")
	IsSync = file.Section("client").Key("isSync").MustBool(true)
	SzIP = file.Section("client").Key("SzIP").MustString("127.0.0.1")
	MoniterTime = file.Section("client").Key("moniterTime").MustInt(300)
	PageSize = file.Section("client").Key("pagesize").MustInt(100)
}
